package com.goods.business.service.imp;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.goods.business.converter.ProductCategoryConverter;
import com.goods.business.mapper.ProductCategoryMapper;
import com.goods.business.mapper.ProductMapper;
import com.goods.business.service.ProductCategoryService;
import com.goods.common.error.BusinessCodeEnum;
import com.goods.common.error.BusinessException;
import com.goods.common.model.business.Product;
import com.goods.common.model.business.ProductCategory;
import com.goods.common.model.system.Role;
import com.goods.common.response.ResponseBean;
import com.goods.common.utils.CategoryTreeBuilder;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.ProductCategoryTreeNodeVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Title:
 * Author: fusu
 * DateTime:  2023/3/24
 * Description:
 **/
@Service
public class ProductCategoryServiceImpl implements ProductCategoryService {

    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private ProductMapper productMapper;

    /**
     * @return com.goods.common.vo.system.PageVO<com.goods.common.vo.business.ProductCategoryTreeNodeVO>
     * @Author: fusu
     * @Description: 分页查询物资类别（封装子节点）
     * @DateTime:10:48 2023/3/24
     * @params: [pageNum, pageSize]
     */
    @Override
    public PageVO<ProductCategoryTreeNodeVO> suppliesCategoryTree(Integer pageNum, Integer pageSize) {
//        Example o = new Example(ProductCategory.class);
//        o.createCriteria().andEqualTo("pid", 0);
        List<ProductCategory> productCategoryList = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = ProductCategoryConverter.converterToProductCategoryTreeNodeVOList(productCategoryList);
        productCategoryTreeNodeVOS = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        //TODO 封装层级
//        if (!CollectionUtils.isEmpty(productCategoryTreeNodeVOS)) {
//            productCategoryTreeNodeVOS = productCategoryTreeNodeVOS.stream().filter(productCategoryTreeNodeVO -> {
//                return productCategoryTreeNodeVO.getPid().intValue() == 0;
//            }).collect(Collectors.toList());
//        }
        PageInfo<ProductCategoryTreeNodeVO> info = new PageInfo<>(productCategoryTreeNodeVOS);
        if (pageNum != null && pageSize != null) {
            productCategoryTreeNodeVOS = ListPageUtils.page(productCategoryTreeNodeVOS, pageSize, pageNum);
        }
        long total = info.getTotal();
        return new PageVO<>(total, productCategoryTreeNodeVOS);
    }

    /**
     * @return void
     * @Author: fusu
     * @Description: 物资分类列表添加
     * @DateTime:11:44 2023/3/24
     * @params: [productCategory]
     */
    @Override
    public void addProductCategory(ProductCategory productCategory) {
        if (productCategory != null) {
            productCategory.setCreateTime(new Date());
            productCategory.setModifiedTime(new Date());
            productCategoryMapper.insert(productCategory);
        }
    }

    /**
     * @param pcId
     * @return com.goods.common.model.business.ProductCategory
     * @Author: fusu
     * @Description: 物资分类列表根据id获取
     * @DateTime:11:51 2023/3/24
     * @params: []
     */
    @Override
    public ProductCategory getProductCategory(Long pcId) {
        ProductCategory productCategory = productCategoryMapper.selectByPrimaryKey(pcId);
        return productCategory;
    }

    /**
     * @return com.goods.common.vo.business.ProductCategoryTreeNodeVO
     * @Author: fusu
     * @Description: 获取分类树
     * @DateTime:14:22 2023/3/24
     * @params: []
     */
    @Override
    public List<ProductCategoryTreeNodeVO> getParentCategoryTree() {
        List<ProductCategory> productCategoryList = productCategoryMapper.selectAll();
        List<ProductCategoryTreeNodeVO> productCategoryTreeNodeVOS = ProductCategoryConverter.converterToProductCategoryTreeNodeVOList(productCategoryList);
        productCategoryTreeNodeVOS = CategoryTreeBuilder.build(productCategoryTreeNodeVOS);
        return productCategoryTreeNodeVOS;
    }

    /**
     * @return void
     * @Author: fusu
     * @Description: 根据主键id更改分类
     * @DateTime:14:47 2023/3/24
     * @params: [productCategory]
     */
    @Override
    public void updateByPcId(ProductCategory productCategory) {
        productCategory.setModifiedTime(new Date());
        productCategoryMapper.updateByPrimaryKey(productCategory);
    }

    /**
     * @return void
     * @Author: fusu
     * @Description: 根据主键id删除物资分类
     * @DateTime:14:52 2023/3/24
     * @params: [pcId]
     */
    @Override
    public void deleteByPcId(Long pcId) throws BusinessException{
//        productCategoryMapper.deleteByPrimaryKey(pcId);
        Example example = new Example(ProductCategory.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("pid", pcId);
        int count = productCategoryMapper.selectCountByExample(example);
        if (count > 0) {
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"存在子节点删除失败");
        }
        Example example1 = new Example(Product.class);
        Example.Criteria criteria1 = example1.createCriteria();
        criteria1.andEqualTo("oneCategoryId", pcId);
        int count1 = productMapper.selectCountByExample(example1);
        if (count1 > 0) {
            throw new BusinessException(BusinessCodeEnum.PARAMETER_ERROR,"存在物资引用删除失败");
        }
        productCategoryMapper.deleteByPrimaryKey(pcId);
    }
}
